<template>
	<view>
		<view class="search-box">
			 <!-- 使用 uni-ui 提供的搜索组件 -->
			  <uni-search-bar placeholder="请输入搜索内容" @input="input" @clear="clearInput" :radius="100" cancelButton="none"></uni-search-bar>
		</view>
		<view class="sugg-list" v-if="searchResults.length !== 0">
			<view class="sugg-item" v-for="(good,index) in searchResults"  @click="gotoDetail(good.goods_id)" :key="index">
				<view class="goods-name">{{ good.goods_name }}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="cleanHistory"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag style="margin-top: 10px;"  v-for="(item, i) in historyList" :key="i" :text="item" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				kw: '',
				timer: null,
				searchResults: [],//// 搜索结果列表
				historyList: []//搜索历史记录
			}
		},
		onLoad () {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods: {
			input(e) {
			    // e.value 是最新的搜索内容
			    
				if (this.timer !== null) {
					clearTimeout(this.timer)
				}
				this.timer = setTimeout(() =>{
					this.kw = e
					this.getSearchList()
				},500)
			},
			// 清空输入框
			clearInput () {
				this.searchResults = []
			},
			// 根据搜索关键词，搜索商品建议列表
			async getSearchList () {
				// 判断关键词是否为空
				if (this.kw === "") {
					this.searchResult = []
					return
				}
				// 发起请求，获取搜索建议列表
				const { data:res } = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.kw })
				 if (res.meta.status !== 200) return uni.$showMsg()
				 this.searchResults = res.message
				 if (this.historyList.indexOf(this.kw) >=0) {
					 return 
				 }
				 this.historyList.unshift(this.kw)
				 this.saveSearchHistory()
			},
			saveSearchHistory () {
				 // 调用 uni.setStorageSync(key, value) 将搜索历史记录持久化存储到本地
				  uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			// 清空历史记录
			cleanHistory () {
				uni.showModal({
				    title: '确认删除',
				    content: '确认删除搜索历史吗？',
				    success: res=> {
				        if (res.confirm) {
				            this.historyList = []
				            uni.removeStorageSync('kw')
				        } else if (res.cancel) {
				            return
				        }
				    }
				});
				
			},
			gotoGoodsList (kw) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + kw
				})
			},
			// 跳转商品详情页
			gotoDetail (id) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + id
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.search-box {
  position: sticky;
  top: 0;
  z-index: 999;
}
.sugg-list {
	padding: 0 5px;
	.sugg-item {
		font-size: 12px;
		padding: 13px 0;
		border: 1px solid #efefef;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.goods-name {
			padding:5px;
			text-align: left;
			white-space: nowrap;
			text-overflow: ellipsis;
			overflow: hidden;
			margin-right: 3px;
		}
	}
}
.history-box {
	padding: 0 5px;
	.history-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 40px;
		font-size: 13px;
		border-bottom: 1px solid #efefef;
	}
	.history-list {
		display: flex;
		flex-wrap: wrap;
		margin-top: 10px;
		/deep/.uni-tag {
		    margin-right: 10px;
			margin-top: 10px;
		}
	}
}
</style>
